home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / opengl / sharedbox / glbox.cpp.z / glbox.cpp
C/C++ Source or Header  |  2002-04-08  |  5KB  |  213 lines

  1. /****************************************************************************
  2. ** $Id:  qt/glbox.cpp   3.0.3   edited Oct 12 12:18 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. /****************************************************************************
  12. **
  13. ** This is a simple QGLWidget displaying a box
  14. **
  15. ** The OpenGL code is mostly borrowed from Brian Pauls "spin" example
  16. ** in the Mesa distribution
  17. **
  18. ****************************************************************************/
  19.  
  20. #include "glbox.h"
  21.  
  22. // Initialize static class variables:
  23.  
  24. // Shared display list id:
  25. GLuint GLBox::sharedDisplayList = 0;
  26.  
  27. // Counter keeping track of number of GLBox instances sharing 
  28. // the display list, so that the last instance can delete it:
  29. int GLBox::sharedListUsers = 0;
  30.  
  31.  
  32. /*!
  33.   Create a GLBox widget
  34. */
  35.  
  36. GLBox::GLBox( QWidget* parent, const char* name, const QGLWidget* shareWidget )
  37.     : QGLWidget( parent, name, shareWidget )
  38. {
  39.     xRot = yRot = zRot = 0.0;        // default object rotation
  40.     scale = 1.0;            // default object scale
  41.     object = 0;
  42.     localDisplayList = 0;
  43. }
  44.  
  45.  
  46. /*!
  47.   Set up the OpenGL rendering state. Robustly access shared display list.
  48. */
  49.  
  50. void GLBox::initializeGL()
  51. {
  52.     // Let OpenGL clear to black
  53.     qglClearColor( black ); 
  54.  
  55.     glEnable(GL_DEPTH_TEST);
  56.  
  57.     if ( sharedListUsers == 0 ) {    // No shared list has been made yet
  58.     sharedDisplayList = makeObject();    // Make one
  59.     object = sharedDisplayList;        // Use it
  60.     sharedListUsers++;                // Keep reference count
  61.     qDebug( "GLBox %s created shared display list.", name() );
  62.     }
  63.     else {                // There is a shared diplay list
  64.     if ( isSharing() ) {        // Can we access it?
  65.         object = sharedDisplayList;        // Yes, use it
  66.         sharedListUsers++;            // Keep reference count
  67.         qDebug( "GLBox %s uses shared display list.", name() );
  68.     }
  69.     else {                
  70.         localDisplayList = makeObject();    // No, roll our own
  71.         object = localDisplayList;        // and use that
  72.         qDebug( "GLBox %s uses private display list.", name() );
  73.     }
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. /*!
  80.   Release allocated resources
  81. */
  82.  
  83. GLBox::~GLBox()
  84. {
  85.     makeCurrent();                // We're going to do gl calls
  86.     if ( localDisplayList != 0 ) {        // Did we make our own?
  87.     glDeleteLists( localDisplayList, 1 );    // Yes, delete it
  88.     qDebug( "GLBox %s deleted private display list.", name() );
  89.     }
  90.     else {
  91.     sharedListUsers--;    // No, we used the shared one; keep refcount
  92.     if ( sharedListUsers == 0 ) {             // Any sharers left?
  93.         glDeleteLists( sharedDisplayList, 1 );    // No, delete it
  94.         sharedDisplayList = 0;
  95.         qDebug( "GLBox %s deleted shared display list.", name() );
  96.     }
  97.     }
  98. }
  99.  
  100.  
  101. /*!
  102.   Paint the box. The actual openGL commands for drawing the box are
  103.   performed here.
  104. */
  105.  
  106. void GLBox::paintGL()
  107. {
  108.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  109.  
  110.     glMatrixMode( GL_MODELVIEW );
  111.     glLoadIdentity();
  112.     glTranslatef( 0.0, 0.0, -3.0 );
  113.     glScalef( scale, scale, scale );
  114.  
  115.     glRotatef( xRot, 1.0, 0.0, 0.0 ); 
  116.     glRotatef( yRot, 0.0, 1.0, 0.0 ); 
  117.     glRotatef( zRot, 0.0, 0.0, 1.0 );
  118.  
  119.     glCallList( object );
  120. }
  121.  
  122.  
  123.  
  124.  
  125. /*!
  126.   Set up the OpenGL view port, matrix mode, etc.
  127. */
  128.  
  129. void GLBox::resizeGL( int w, int h )
  130. {
  131.     glViewport( 0, 0, (GLint)w, (GLint)h );
  132.     glMatrixMode( GL_PROJECTION );
  133.     glLoadIdentity();
  134.     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  135. }
  136.  
  137.  
  138. /*!
  139.   Generate an OpenGL display list for the object to be shown, i.e. the box
  140. */
  141.  
  142. GLuint GLBox::makeObject()
  143. {
  144.     GLuint list;
  145.  
  146.     list = glGenLists( 1 );
  147.  
  148.     glNewList( list, GL_COMPILE );
  149.  
  150.     glBegin(GL_QUADS);
  151.     /* Front face */
  152.     qglColor( green );
  153.     glVertex3f(-1.0, 1.0, 1.0);
  154.     glVertex3f(1.0, 1.0, 1.0);
  155.     glVertex3f(1.0, -1.0, 1.0);
  156.     glVertex3f(-1.0, -1.0, 1.0);
  157.     /* Back face */
  158.     qglColor( yellow );
  159.     glVertex3f(-1.0, 1.0, -1.0);
  160.     glVertex3f(1.0, 1.0, -1.0);
  161.     glVertex3f(1.0, -1.0, -1.0);
  162.     glVertex3f(-1.0, -1.0, -1.0);
  163.     /* Top side face */
  164.     qglColor( blue );
  165.     glVertex3f(-1.0, 1.0, 1.0);
  166.     glVertex3f(1.0, 1.0, 1.0);
  167.     glVertex3f(1.0, 1.0, -1.0);
  168.     glVertex3f(-1.0, 1.0, -1.0);
  169.     /* Bottom side face */
  170.     qglColor( red );
  171.     glVertex3f(-1.0, -1.0, 1.0);
  172.     glVertex3f(1.0, -1.0, 1.0);
  173.     glVertex3f(1.0, -1.0, -1.0);
  174.     glVertex3f(-1.0, -1.0, -1.0);
  175.     glEnd();
  176.     glEndList();
  177.  
  178.     return list;
  179. }
  180.  
  181.  
  182. /*!
  183.   Set the rotation angle of the object to \e degrees around the X axis.
  184. */
  185.  
  186. void GLBox::setXRotation( int degrees )
  187. {
  188.     xRot = (GLfloat)(degrees % 360);
  189.     updateGL();
  190. }
  191.  
  192.  
  193. /*!
  194.   Set the rotation angle of the object to \e degrees around the Y axis.
  195. */
  196.  
  197. void GLBox::setYRotation( int degrees )
  198. {
  199.     yRot = (GLfloat)(degrees % 360);
  200.     updateGL();
  201. }
  202.  
  203.  
  204. /*!
  205.   Set the rotation angle of the object to \e degrees around the Z axis.
  206. */
  207.  
  208. void GLBox::setZRotation( int degrees )
  209. {
  210.     zRot = (GLfloat)(degrees % 360);
  211.     updateGL();
  212. }
  213.